package com.carkeeper.user.service.impl;

import com.carkeeper.user.dao.CarMapper;
import com.carkeeper.user.pojo.Car;
import com.carkeeper.user.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;

    /*
     * 根据userId查car集合
     * */

    @Override
    public List<Car> findByUserId(Integer userId) {
        if (userId == null && "".equals(userId)){
            throw  new RuntimeException("车主id不存在");
        }

        Example example = new Example(Car.class);
        Example.Criteria criteria = example.createCriteria();
        //精确查询
        criteria.andEqualTo("userId",userId);
        //根据userId 查询car
        List<Car> list = carMapper.selectByExample(example);
        return list;
    }

    /*
     * 根据tb_order表的car_code查car信息
     * */
    @Override
    public Car findByOrderCode(String code) {
        if (code == null){
            throw new RuntimeException("信息不存在");
        }
        //创建car
        Car car = new Car();
        //封装code
        car.setCode(code);
        //查询car
        car = carMapper.selectOne(car);
        return car;
    }
    /*
     * 查询所有car
     * */
    @Override
    public List<Car> findAll() {
        //查所有
        List<Car> list = carMapper.selectAll();
        return list;
    }
}
